

function getname(key) {
    // 转url地址
    let strData = location.href.split('?')[1];
    let arrData = strData.split('=')
    // console.log(arrData[1]);
    return decodeURI(arrData[1])
}


function getGoodlist() {
    // 假设getname函数已经正确实现了获取URL参数的功能
    const keyword = getname("name");
    // console.log(keyword);

    // 如果keyword存在，构建请求数据，否则直接返回
    ajaxUsePromise02({
        url: `http://localhost:3000/books`,
        method: "get",
        data: `keyword=${encodeURIComponent(keyword)}`
    }).then(res => {
        for (let i = 0; i < res.length; i++) {
            if (keyword == res[i].id) {
                // console.log(res[i]);
                showlist(res[i])
            }
        }
    });
}

// 渲染数据
function showlist(data) {
    let listshowrname = document.querySelector(".list-showrname")
    let listshowrauthor = document.querySelector(".list-showrauthor")
    let listshowrpic = document.querySelector(".list-showrpic")
    let subtotalpic = document.querySelector(".subtotal-pic")
    let footpic = document.querySelector(".foot-pic")
    let listshowimg = document.querySelector(".list-showl img")

    listshowrname.innerHTML = `书籍名称《${data.name}》`
    listshowrauthor.innerHTML = `作者：${data.author}`
    listshowrpic.innerHTML = `售价：￥${data.price}`
    subtotalpic.innerHTML = `￥${data.price - 5}`
    footpic.innerHTML = `￥${data.price - 5}`
    console.log(data.img);
    listshowimg.src = data.img;
    document.querySelector(".fanhui").onclick = function () {
        location.href = `./list.html?category=${data.id}`
    }


    layui.use(function () {
        var layer = layui.layer;
        var util = layui.util;
        // 事件
        util.on('lay-on', {
            "test-confirm": function () {
                layer.confirm('确认支付？', { icon: 3 }, function () {
                    layer.msg('支付成功', { icon: 1 });
                    let currentTime = gettime()
                    saveCookie("paytime", currentTime, 7);
                    setInterval(function () {
                        location.href = `./pay.html?category=${data.id}`
                    }, 1000)

                }, function () {
                    // layer.msg('点击取消的回调');
                });
            }
        })
    });
}

function gettime() {
    // 创建一个Date对象
    var now = new Date();

    // 获取年、月、日、小时、分钟和秒信息
    var year = now.getFullYear(); // 年
    var month = now.getMonth() + 1; // 月，JavaScript中月份是从0开始的，所以需要+1
    var day = now.getDate(); // 日
    var hour = now.getHours(); // 小时
    var minute = now.getMinutes(); // 分钟
    var second = now.getSeconds(); // 秒

    // 格式化日期和时间
    var formattedDate = year + '-' + (month < 10 ? '0' + month : month) + '-' + (day < 10 ? '0' + day : day);
    var formattedTime = hour + ':' + (minute < 10 ? '0' + minute : minute) + ':' + (second < 10 ? '0' + second : second);


    time = formattedDate + " " + formattedTime
    console.log(time);
    return time

}

function shouji() {
    let myiphone = document.querySelector('.iphone')
    let userphone = getCookie("userphone");
    myiphone.value = userphone
}


window.onload = function () {
    shouji()
    getname();
    getGoodlist();
}